package remote.collections
{
	import remote.ISelect;
	import remote.core.IMultiSelect;
	
	
	/**
	 * 可选对象组，对于组中实现ISelect接口的对象，可以选中一个或多个。
	 * maxSelectedCount决定了最大选中数量，选中对象的数量超过此值时，早期被选中的对象被取消选中
	 * @author 银魂飞雪
	 * @createDate 2011-3-31
	 */
	public class SelectGroup implements IList, IMultiSelect
	{
		/**
		 * @private
		 * 子项集合 
		 */		
		protected var _items:Array;
		
		/**
		 * @private
		 * 选中项集合 
		 */		
		protected var _selectedItems:Array;
		
		/**
		 * @private
		 * 最大选中数量 
		 */		
		protected var _maxSelected:int = 1;
		public function SelectGroup()
		{
			_items = new Array();
			_selectedItems = new Array();
		}
		
		/**
		 * 选中对象 
		 * @param item
		 * 
		 */		
		public function selectItem(item:ISelect):void
		{
			if(containsSelected(item))
				return;
			item.selected = true;
			_selectedItems.push(item);
			removeOverTopSelectedItems();
		}
		
		/**
		 * 按序号选中对象
		 * @param index
		 * 
		 */		
		public function selectItemAt(index:int):void
		{
			selectItem(getItemAt(index) as ISelect);
		}
		
		/**
		 * 取消选中 
		 * @param item
		 * 
		 */		
		public function unselectItem(item:ISelect):void
		{
			if(containsSelected(item))
			{
				item.selected = false;
				_selectedItems.splice(_selectedItems.indexOf(item), 1);
			}
		}
		
		/**
		 * 取消选中 
		 * @param index
		 * 
		 */		
		public function unselectItemAt(index:int):void
		{
			unselectItem(getItemAt(index) as ISelect);
		}
		
		/**
		 * 取消所有选中 
		 * 
		 */		
		public function unselectAll():void
		{
			for each(var item:ISelect in _selectedItems)
			{
				item.selected = false;
			}
			_selectedItems.splice(0, _selectedItems.length);
				
		}
		
		/**
		 * 是否包含某项 
		 * @param item
		 * @return 
		 * 
		 */		
		private function contains(item:Object):Boolean
		{
			return getItemIndex(item) != -1;
		}
		
		/**
		 * 选中集合中是否包含某项 
		 * @param item
		 * @return 
		 * 
		 */		
		private function containsSelected(item:Object):Boolean
		{
			return _selectedItems.indexOf(item) != -1;
		}
		
		/**
		 * 清除超出最大可选中数量的项，优先取消早期选中的对象 
		 * 
		 */		
		private function removeOverTopSelectedItems():void
		{
			while(selectedLength > maxSelectedCount)
			{
				(_selectedItems[0] as ISelect).selected = false;
				_selectedItems.splice(0, 1);
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */		
		public function get length():int
		{
			return _items.length;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */		
		public function addItem(item:Object):void
		{
			addItemAt(item, length);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function addItemAt(item:Object, index:int):void
		{
			var btn:ISelect = item as ISelect;
			if(getItemIndex(item) == -1)
			{
				_items.splice(index, 0, btn);
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function removeItem(item:Object):Object
		{
			return removeItemAt(getItemIndex(item));
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function removeItemAt(index:int):Object
		{
			if(containsSelected(_items[index]))
				this.unselectItemAt(index);
			return  _items.splice(index, 1)[0];
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function getItemAt(index:int):Object
		{
			return _items[index];
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function getItemIndex(item:Object):int
		{
			return _items.indexOf(item);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function removeAll():void
		{
			while(length > 0)
				removeItemAt(0);
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function setItemAt(item:Object, index:int):Object
		{
			removeItem(item);
			addItemAt(item, index);
			return item;
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function toArray():Array
		{
			return _items.concat();
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function get maxSelectedCount():int
		{
			return _maxSelected;
		}
		
		public function set maxSelectedCount(value:int):void
		{
			if(_maxSelected != value)
			{
				_maxSelected = value;
				removeOverTopSelectedItems();
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function get selectedItems():Array
		{
			return _selectedItems;
		}
		
		public function set selectedItems(value:Array):void
		{
			unselectAll();
			var len:int = Math.min(value.length, maxSelectedCount);
			for(var i:int = 0; i < len; i++)
			{
				selectItem(value[i]);	
			}
		}
		
		/**
		 * @inheritDoc 
		 * 
		 */	
		public function get selectedIndexs():Array
		{
			var result:Array = new Array();
			for(var i:int = 0; i < selectedLength; i++)
			{
				result.push(this.getItemIndex(this._selectedItems[i]));
			}
			return result;
		}
		
		public function set selectedIndexs(value:Array):void
		{
			unselectAll();
			var len:int = Math.min(value.length, maxSelectedCount);
			for(var i:int = 0; i < len; i++)
			{
				selectItemAt(value[i]);	
			}
		}
		
		/**
		 * 已选中的数量
		 * 
		 */	
		public function get selectedLength():int
		{
			return _selectedItems.length;
		}
	}
}